001    /**
002     * Java Gui Builder - A library to build GUIs using an XML file.
003     * Copyright 2002, 2003 (C) François Beausoleil
004     *
005     * This library is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU Lesser General Public
007     * License as published by the Free Software Foundation; either
008     * version 2.1 of the License, or (at your option) any later version.
009     *
010     * This library is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013     * Lesser General Public License for more details.
014     *
015     * You should have received a copy of the GNU Lesser General Public
016     * License along with this library; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018     */
019    
020    package jgb.builder.utils;
021    
022    import java.lang.reflect.Constructor;
023    
024    /**
025     * @since 0.2a
026     * @author Francois Beausoleil, <a href="mailto:fbos@users.sourceforge.net">fbos@users.sourceforge.net</a>
027     */
028    public class ConstructorCall extends ParametersAccumulator {
029        private Class calledClass;
030    
031        public ConstructorCall(String className) throws ClassNotFoundException {
032            calledClass = Class.forName(className);
033        }
034    
035        public Object call() throws NoSuchMethodException, MethodCallException {
036            try {
037                Object[] values = createValuesArray();
038                return findConstructor().newInstance(values);
039            } catch (NoSuchMethodException e) {
040                throw e;
041            } catch (Exception e) {
042                throw new MethodCallException("Could not instantiate object with parameters "
043                        + getParameterTypes().toString() + " - " + getParameterValues().toString(), e);
044            }
045        }
046    
047        private Constructor findConstructor() throws NoSuchMethodException {
048            Class[] callParams = createParametersArray();
049    
050            Constructor[] ctors = calledClass.getConstructors();
051            for (int i = 0; i < ctors.length; i++) {
052                Constructor ctor = ctors[i];
053    
054                Class[] ctorParams = ctor.getParameterTypes();
055                if (ctorParams.length == callParams.length) {
056                    boolean isValidConstructor = true;
057    
058                    for (int j = 0; j < ctorParams.length; j++) {
059                        Class ctorParam = ctorParams[j];
060                        Class callParam = callParams[j];
061    
062                        if (ctorParam.isAssignableFrom(callParam) == false) {
063                            isValidConstructor = false;
064                            break;
065                        }
066                    }
067    
068                    if (isValidConstructor) {
069                        return ctor;
070                    }
071                }
072            }
073    
074            throw new NoSuchMethodException("Unable to find constructor with specified parameters");
075        }
076    }